package phantrang;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import trangchu.UtilDelegate;

import form.DSJ_FormMuaHang;
import form.DSJ_XuatHang;


public class PT_MuaHang {
	public Vector<DSJ_FormMuaHang> getListDoiTac(Integer soTrang) throws SQLException
    {
            Vector<DSJ_FormMuaHang> result = new Vector<DSJ_FormMuaHang>();
            Connection con =UtilDelegate.getConnection();
            try
            {
                    CallableStatement cst = con.prepareCall("{call DanhSach_MuaHang(?)}");
//					System.out.println(getsoDoiTac());
                    /*
					if (getsoDoiTac() < soTrang*25||soTrang*25<=0) {
                    	cst.setInt(1, 0);
					}else cst.setInt(1, soTrang);
                    */
					cst.setInt(1, soTrang);
                    ResultSet rs =  cst.executeQuery();

                    while(rs.next())
                    {
                    	DSJ_FormMuaHang varmuahang = new DSJ_FormMuaHang();
                    	varmuahang.setMaSP(rs.getString("HangHoa_ID"));
                    	varmuahang.setTenSP(rs.getString("TenHangHoa"));
                    	varmuahang.setKho(rs.getString("KhoHang"));
                    	varmuahang.setDonViSP(rs.getString("TenDonVi"));
                    	varmuahang.setGiaVon(rs.getString("DonGia"));
                    	varmuahang.setSoLuong(rs.getString("SoLuong"));
                    	varmuahang.setThanhTien(rs.getString("ThanhTien"));
                    	varmuahang.setNhapHang_ID(rs.getString("NhapHang_ID"));
                        
                        result.add(varmuahang);
                    }

            }
            catch (Exception e)
            {
                    e.printStackTrace();
                    result = null;
            }
            finally
            {
                    UtilDelegate.closeConnection(con);	
                    return result;
            }
    }
    public Integer getsoDoiTac() throws SQLException
    {
    		Integer result = null;
    		Connection con =UtilDelegate.getConnection();
            try
            {
                    
                    CallableStatement soLuongRecod = con.prepareCall("{call SoLuong_MuaHang()}");
                    ResultSet rs1 =  soLuongRecod.executeQuery();
					rs1.next();
//					System.out.println(rs1.getInt(1));
					result = rs1.getInt(1);
            }
            catch (Exception e)
            {
                    e.printStackTrace();
                    result = null;
            }
            finally
            {
                    UtilDelegate.closeConnection(con);	
                    return result;
            }
    }
    
    
    
    
    
    public Vector<DSJ_FormMuaHang> getListXuatHang(Integer soTrang) throws SQLException
    {
            Vector<DSJ_FormMuaHang> result = new Vector<DSJ_FormMuaHang>();
            Connection con =UtilDelegate.getConnection();
            try
            {
                    CallableStatement cst = con.prepareCall("{call DanhSach_XuatHang(?)}");
//					System.out.println(getsoDoiTac());
                    /*
					if (getsoDoiTac() < soTrang*25||soTrang*25<=0) {
                    	cst.setInt(1, 0);
					}else cst.setInt(1, soTrang);
                    */
					cst.setInt(1, soTrang);
                    ResultSet rs =  cst.executeQuery();

                    while(rs.next())
                    {
                    	DSJ_FormMuaHang varmuahang = new DSJ_FormMuaHang();
                    	varmuahang.setMaSP(rs.getString("HangHoa_ID"));
                    	varmuahang.setTenSP(rs.getString("TenHangHoa"));
                    	varmuahang.setKho(rs.getString("KhoHang"));
                    	varmuahang.setDonViSP(rs.getString("TenDonVi"));
                    	varmuahang.setGiaVon(rs.getString("DonGia"));
                    	varmuahang.setSoLuong(rs.getString("SoLuong"));
                    	varmuahang.setThanhTien(rs.getString("ThanhTien"));
                    	varmuahang.setXuatHang_ID(rs.getString("XuatHang_ID"));
                        
                        result.add(varmuahang);
                    }

            }
            catch (Exception e)
            {
                    e.printStackTrace();
                    result = null;
            }
            finally
            {
                    UtilDelegate.closeConnection(con);	
                    return result;
            }
    }
    public Integer getsoXuatHang() throws SQLException
    {
    		Integer result = null;
    		Connection con =UtilDelegate.getConnection();
            try
            {
                    
                    CallableStatement soLuongRecod = con.prepareCall("{call SoLuong_XuatHang()}");
                    ResultSet rs1 =  soLuongRecod.executeQuery();
					rs1.next();
//					System.out.println(rs1.getInt(1));
					result = rs1.getInt(1);
            }
            catch (Exception e)
            {
                    e.printStackTrace();
                    result = null;
            }
            finally
            {
                    UtilDelegate.closeConnection(con);	
                    return result;
            }
    }
}
